package com.gltc.util;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;

public class DetectFaceDemo {
	public String run(HttpServletRequest request,String filePath) {
		int lageMin = Integer.parseInt(request.getParameter("lageMin"));
		int lageMax = Integer.parseInt(request.getParameter("lageMax"));
		int mywidth = Integer.parseInt(request.getParameter("mywidth"));//照片宽（比例）
		int myheight = Integer.parseInt(request.getParameter("myheight"));//照片高（比例）
		int fMinWidth = Integer.parseInt(request.getParameter("fMinWidth"));//人脸宽度比重_最小
		int fMaxWidth = Integer.parseInt(request.getParameter("fMaxWidth"));//人脸宽度比重_最大
		int lrWeight = Integer.parseInt(request.getParameter("lrWeight"));//人脸左右最大比重
		
		//当前类的同一目录下获取文件路径
		//String xmlfilePath = getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
		
		//classpath路径下获取文件
		String xmlfilePath = getClass().getClassLoader().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
		CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
		
		File file = new File(filePath);
		System.out.println("图片大小为"+file.length()/1024+"kb");
		if (file.length() < lageMin*1024 
				|| file.length() > lageMax*1024) {
			return "图片大小不符合条件,实际为"+file.length()/1024+"kb";
		}
		
		Mat image = Highgui.imread(filePath);
		
		MatOfRect faceDetections = new MatOfRect();
		faceDetector.detectMultiScale(image, faceDetections);
		
		System.out.println("高height:" + image.height());
		System.out.println("宽width:" + image.width());
		//宽高比例
		//参数宽/参数高 == 实际宽 /实际高
		
		if (mywidth*image.height() != myheight * image.width()) {
			return "图片宽高比不符合要求";
		}
		
		Rect[] r = faceDetections.toArray();
		System.out.println("检测的人脸数量："+r.length);
		if (r.length!=1) {
			return "检测的人脸数量不正确，检测数为"+r.length;
		}
		
		// Draw a bounding box around each face.
		for (Rect rect : faceDetections.toArray()) {
			Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
					+ rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
			System.out.println("脸部-起始点-x:" + rect.x);
			System.out.println("脸部-起始点-y:" + rect.y);
			System.out.println("脸部-宽度-width:" + rect.width);
			if (rect.width < fMinWidth*image.width()/mywidth) {
				return "头像比重偏小";
			}
			if (rect.width > fMaxWidth*image.width()/mywidth) {
				return "头像比重偏大";
			}
			//脸距左边距 = rect.x 
			//脸距右边距 = image.width()-rect.x-rect.width
			if (rect.x - (image.width()-rect.x-rect.width)>image.width()*lrWeight/mywidth) {
				return "头像偏右";
			}
			if ((image.width()-rect.x-rect.width) - rect.x >image.width()*lrWeight/mywidth) {
				return "头像偏左";
			}
		}
		//图片背景颜色 是否是白亮色  
		boolean boo = MyImageUtil.isWhiteColor(filePath, 10, 10, 20, 20 );
		if (!boo) {
			return "图片背景不是白色";
		}
		return "1";
		//复制描绘脸部的图片
//		String filename = "faceDetection.png";
//		Highgui.imwrite(filename, image);
	}
}
